<template>
  <a-modal
    :title="title"
    v-model="show"
    :maskClosable="false"
    :width="1050"
    placement="right"
    :closable="true"
    @cancel="onClose"
    :footer="null"
    :visiable="acctEntryAddEditVisiable"
  >
    <a-form :form="form">
      <a-row>
        <a-col :md="12" :sm="24">
          <a-form-item label="序号"
                       v-bind="formItemLayout">
            <a-input v-decorator="['groupNo',{rules: [{ required: true, message: '必填' }]}]" :disabled="true"/>
          </a-form-item>
          <a-form-item label="id"
                       v-bind="formItemLayout" style="display: none">
            <a-input v-decorator="['id']"/>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="24">
          <a-form-item label="借方清算角色"
                       v-bind="formItemLayout">
            <a-select
              mode="default"
              :allowClear="true"
              style="width: 100%"
              show-search
              placeholder="请选择"
              option-filter-prop="children"
              :filter-option="filterOption"
              v-decorator="['settleRoleD',{rules: [{ required: true, message: '必填' }]}]">
              <a-select-option key="01">总行</a-select-option>
              <a-select-option key="02">发卡分行</a-select-option>
              <a-select-option key="03">发卡支行</a-select-option>
              <a-select-option key="04">收单分行</a-select-option>
              <a-select-option key="05">收单支行</a-select-option>
              <a-select-option key="10">商户</a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :md="12" :sm="24">
          <a-form-item label="贷方清算角色"
                       v-bind="formItemLayout">
            <a-select
              mode="default"
              :allowClear="true"
              style="width: 100%"
              show-search
              placeholder="请选择"
              option-filter-prop="children"
              :filter-option="filterOption"
              v-decorator="['settleRoleC',{rules: [{ required: true, message: '必填' }]}]">
              <a-select-option key="01">总行</a-select-option>
              <a-select-option key="02">发卡分行</a-select-option>
              <a-select-option key="03">发卡支行</a-select-option>
              <a-select-option key="04">收单分行</a-select-option>
              <a-select-option key="05">收单支行</a-select-option>
              <a-select-option key="10">商户</a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="24">
          <a-form-item label="借方科目"
                       v-bind="formItemLayout">
            <a-select
              show-search
              placeholder="请输入代码或名称"
              option-filter-prop="children"
              style="width: 334px"
              :filter-option="filterOption"
              @focus="selectSettleAcctDesc"
              @search="selectSettleAcctDesc"
              @select="setSettleAcctIdDValue"
              v-decorator="['settleAcctDescD',{rules: [{ required: true, message: '必填字段'}]}]"
            >
              <a-select-option v-for="(d,index) in settleAcctIdData" :key="index">
                {{ d.name }}
              </a-select-option>
            </a-select>
          </a-form-item>
          <a-form-item label="settleAcctIdD"
                       v-bind="formItemLayout" style="display: none">
            <a-input v-decorator="['settleAcctIdD']"/>
          </a-form-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :md="12" :sm="24">
          <a-form-item label="贷方科目"
                       v-bind="formItemLayout">
            <a-select
              show-search
              placeholder="请输入代码或名称"
              option-filter-prop="children"
              style="width: 334px"
              :filter-option="filterOption"
              @focus="selectSettleAcctDesc"
              @search="selectSettleAcctDesc"
              @select="setSettleAcctIdCValue"
              v-decorator="['settleAcctDescC',{rules: [{ required: true, message: '必填字段'}]}]"
            >
              <a-select-option v-for="(d,index) in settleAcctIdData" :key="index">
                {{ d.name }}
              </a-select-option>
            </a-select>
          </a-form-item>
          <a-form-item label="settleAcctIdC"
                       v-bind="formItemLayout" style="display: none">
            <a-input v-decorator="['settleAcctIdC']"/>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="24">
          <a-form-item label="借方科目金额方向"
                       v-bind="formItemLayout">
            <a-select
              mode="default"
              :allowClear="true"
              style="width: 100%"
              show-search
              placeholder="请选择"
              option-filter-prop="children"
              :filter-option="filterOption"
              v-decorator="['amountDirectionD',{rules: [{ required: true, message: '必填' }]}]">
              <a-select-option key="+">正金额</a-select-option>
              <a-select-option key="-">负金额</a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :md="12" :sm="24">
          <a-form-item label="贷方科目金额方向"
                       v-bind="formItemLayout">
            <a-select
              mode="default"
              :allowClear="true"
              style="width: 100%"
              show-search
              placeholder="请选择"
              option-filter-prop="children"
              :filter-option="filterOption"
              v-decorator="['amountDirectionC',{rules: [{ required: true, message: '必填' }]}]">
              <a-select-option key="+">正金额</a-select-option>
              <a-select-option key="-">负金额</a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
      </a-row>
      <br>

    </a-form>
    <div class="drawer-bootom-button">
      <a-button @click="onClose" type="primary">取消</a-button>
      <a-button @click="handleSubmit" type="primary" :loading="loading">提交</a-button>
    </div>
  </a-modal>
</template>
<script>
  const formItemLayout = {
    labelCol: {span: 8},
    wrapperCol: {span: 16}
  }
  export default {
    name: 'AcctEntryAddEdit',
    props: {
      acctEntryAddEditVisiable: {
        default: false
      },
      instId: '',
    },
    data() {
      return {
        title: '新增会计分录',
        loading: false,
        formItemLayout,
        form: this.$form.createForm(this),
        validateStatus: '',
        settleAcctIdData: [],
        optFlagTmp: '',
        info: {}
      }
    },
    computed: {
      show: {
        get: function () {
          return this.acctEntryAddEditVisiable
        },
        set: function () {
        }
      }
    },
    mounted() {

    },

    methods: {
      filterOption(input, option) {
        return (
          option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
        );
      },

      selectSettleAcctDesc(val) {
        this.settleAcctIdData = []
        let param = {}
        if (val) {
          param = {
            q: encodeURIComponent(val),
            pageSize: 20
          }
        } else {
          param = {
            q: '',
            pageSize: 20
          }
        }
        this.$get('admin/csmc/acctEntry/acctEntryDialog', param).then((r) => {
          let result = r.data.results
          result.forEach(r => {
            this.settleAcctIdData.push(r);
          })
        })
      },
      setSettleAcctIdDValue(index) {
        this.form.setFieldsValue({
          settleAcctIdD: this.settleAcctIdData[index].id,
          settleAcctDescD: this.settleAcctIdData[index].name,
        })
      },

      setSettleAcctIdCValue(index) {
        this.form.setFieldsValue({
          settleAcctIdC: this.settleAcctIdData[index].id,
          settleAcctDescC: this.settleAcctIdData[index].name,
        })
      },

      reset() {
        this.loading = false
        this.form.resetFields()
      },
      onClose() {
        this.reset()
        this.$emit('close')
      },
      handleSubmit() {
        this.form.validateFields((err, values) => {
          if (!err) {
            values.optFlag = this.optFlagTmp
            this.form.resetFields()
            this.$emit('success', values);
          }
        })
      },

      setAcctEntryFormRecordValues({...info}) {
        let fields = ['id', 'groupNo', 'settleRoleD', 'settleRoleC', 'settleAcctIdD', 'settleAcctDescD',
          'settleAcctDescC', 'settleAcctIdC', 'amountDirectionD', 'amountDirectionC']
        this.optFlagTmp = info.optFlag
        if (info.optFlag == 'upd') {
          this.title = '修改会计分录'
        }
        Object.keys(info).forEach((key) => {
          if (fields.indexOf(key) !== -1) {
            this.form.getFieldDecorator(key)
            let obj = {}
            obj[key] = info[key]
            this.form.setFieldsValue(obj)
          }
        })
      },

    }

  }
</script>





